﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web;
using System.Web.UI;
using EPiServer;
using EPiServer.Core;
using EPiServer.Filters;
using EPiServer.Municipality.Extensions;
using EPiServer.Core.Html;

namespace EPiServer.Municipality.Web.UI.WebControls 
{
    public class Rss : Control
    {
        /* *******************************************************************
        *  Properties 
        * *******************************************************************/

        #region public PageReference RssFeedPage
        /// <summary>
        /// The PageId of the RssFeedPage
        /// </summary>
        /// <value></value>
        public PageReference RssFeedPage
        {
            get; set;
        }
        #endregion
        #region public String FeedFormatterType
        /// <summary>
        /// The type of feed to render 
        /// </summary>
        /// <value>Rss20FeedFormatter or Atom10FeedFormatter</value>
        public string FeedFormatterType
        {
            get;
            set;
        }
        #endregion

        /* *******************************************************************
        *  Methods 
        * *******************************************************************/

        #region   protected override void Render(HtmlTextWriter output)
        /// <summary>
        /// 
        /// </summary>
        protected override void Render(HtmlTextWriter output)
        {
            WriteRssDocument();
            base.Render(output);
        }
        #endregion
        #region private void WriteRssDocument() {
        /// <summary>
        /// 
        /// </summary>
        private void WriteRssDocument() {
            var rssPage = DataFactory.Instance.GetPage(RssFeedPage);
            var feed =
                new SyndicationFeed(rssPage["Heading"] as string ?? rssPage.PageName,
                                    rssPage["Description"] as string ?? string.Empty,
                                    new Uri(rssPage.GetExternalUrl()),
                                    rssPage.PageLink.ID.ToString(),
                                    DateTime.Now);

            var children = DataFactory.Instance.GetChildren(RssFeedPage);
            FilterForVisitor.Filter(children);
            var items = new List<SyndicationItem>();

            foreach (var page in children) {
                var item =
                    new SyndicationItem(String.Format("{0}", page.PageName),
                                        String.Format("{0}", TextIndexer.StripHtml(page["MainBody"].ToString(),400)),
                                        new Uri(String.Format("{0}", page.GetExternalUrl())),
                                        page.PageLink.ID.ToString(),
                                        page.Changed);
                items.Add(item);
            }

            feed.Items = items;

            if (FeedFormatterType == "Atom10FeedFormatter")
            {
                var renderAtomResult = new SyndicationResult<Atom10FeedFormatter> {Feed = feed};
                renderAtomResult.ExecuteResult();
            }
            else {
                var renderRssResult = new SyndicationResult<Rss20FeedFormatter> { Feed = feed };
                renderRssResult.ExecuteResult();
            }

        }
        #endregion
    }
}
